package com.zmx.doctor.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zmx.doctor.R;
import com.zmx.doctor.pojo.Give;
import com.zmx.doctor.pojo.TripDay;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 程序猿：胖胖祥
 * 时 间：2020/7/22  13:11
 * 功 能：
 */
public class TripGridviewadapter  extends BaseAdapter {

    private Context context;
    private List<TripDay> lists;

    public TripGridviewadapter(Context context,List<TripDay> lists){
        this.context = context;
        this.lists = lists;
    }

    @Override
    public int getCount() {
        return lists.size();
    }

    @Override
    public Object getItem(int position) {
        return lists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {

        TripGridviewadapter.ViewHolder holder = null;
        if(v == null){

            holder = new TripGridviewadapter.ViewHolder();
            v = LayoutInflater.from(context).inflate(R.layout.adapter_trip_gridview,null);
            holder.week = v.findViewById(R.id.week);
            holder.day = v.findViewById(R.id.day);
            holder.layout = v.findViewById(R.id.layout);
            v.setTag(holder);
        }else{

            holder = (TripGridviewadapter.ViewHolder) v.getTag();

        }

        if (isTheSameDay(lists.get(position).getDate(),new Date())){

            holder.layout.setBackgroundColor(context.getResources().getColor(R.color.there));
            holder.week.setTextColor(context.getResources().getColor(R.color.white));
            holder.day.setTextColor(context.getResources().getColor(R.color.white));

        }else{

            holder.layout.setBackgroundColor(context.getResources().getColor(R.color.main_bg));
            holder.week.setTextColor(context.getResources().getColor(R.color.black));
            holder.day.setTextColor(context.getResources().getColor(R.color.black));

        }

        holder.week.setText(lists.get(position).getWeek());
        holder.day.setText(lists.get(position).getDay());

        return v;

    }

    public class ViewHolder{

        TextView day,week;
        RelativeLayout layout;

    }

    public static boolean isTheSameDay(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
                && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
                && (c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH));
    }

}
